package com.ciis.system.login.controller;

import com.ciis.base.annotation.Auth;
import com.ciis.base.utils.SessionUtils;
import com.ciis.system.login.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-5-24
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginController {

    @Autowired
    ILoginService loginService;

    /**
     * 访问登录页
     * @return
     */
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String loginGet() {
        return "home";
    }

    /**
     * 进入首页后的默认页面
     * @return
     */
    @RequestMapping(value = "/")
    @Auth(verifyLogin = false)
    public String defaultPage(HttpServletRequest request) {
        return logout(request);
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @RequestMapping(value = "/back/logout")
    @Auth(verifyLogin = false)
    public String logout(HttpServletRequest request) {
        SessionUtils.removeUser(request);
        return "login";
    }

    /**
     * 请求登录，验证用户
     * @param request
     * @param name
     * @param password
     * @param code
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView loginPost(HttpServletRequest request, @RequestParam String name, @RequestParam String password,
                                  @RequestParam(value = "code", required = false, defaultValue = "") String code) throws Exception {
        //return loginService.validateLogin(request,name,password,code);
        return null;
    }

    @RequestMapping(value = "/validate", method = RequestMethod.POST)
    @ResponseBody
    @Auth(verifyLogin = false)
    public HashMap<String,String> validateLogin(HttpServletRequest request, @RequestParam String name, @RequestParam String password,
                                  @RequestParam(value = "code", required = false, defaultValue = "") String code) throws Exception {
        return loginService.validateLogin(request,name,password,code);
    }
}
